import axios from "axios";


//封装axios
let instance = axios.create({
  baseURL: "/api",
  timeout: 5000,
});
//添加请求拦截
instance.interceptors.request.use(
  (config) => {

    // console.log(config); //本次请求的配置信息
    //接口文档要求发送/sendSMS请求时 设置请求头为application/x-www-form-urlencoded
    if (config.url === "/sendSMS" || config.url === "/wechatUsers/PCLogin") {
      config.headers["Content-Type"] = "application/x-www-form-urlencoded";
    }
    //请求头携带token
    let myToken = localStorage.getItem("x-auth-token");
    if (myToken) {
      //有的话就携带上token
      config.headers["x-auth-token"] = myToken;
    }
    return config;
  },
  (err) => {
    return Promise.reject(err);
  }
);
//响应拦截
instance.interceptors.response.use(
  (res) => {
    // console.log("每次接收到响应会先执行这里");
    //统一处理响应数据
    if (res.data.code != 0 && res.data.code != 407 && res.data.code != 400) {
      alert(res.data.message);
      

      return false;
    }
    //只返回res.data减少代码书写
    
    return res.data;
  },
  (err) => {
    return Promise.reject(err);
  }
);
export default instance;
